package com.eastedu.superise.handler.common;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.eastedu.repository.permission.po.BaseEntity;
import com.eastedu.superise.handler.ktree.KtreeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * The type Resource update time service.
 *
 * @author zhangzhishuai
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ResourceUpdateTimeService<T extends BaseEntity> {

    private final KtreeService ktreeService;

    public void update(BaseMapper<T> baseMapper, T po, List<String> sourceChapterIds) {
        List<String> chapterNodeIds = this.ktreeService.listChapterNodeIds();
        // 判断 是否有交集
        if (CollectionUtil.isNotEmpty(chapterNodeIds) && CollectionUtil.isNotEmpty(sourceChapterIds)) {
            Collection<String> intersection = CollectionUtil.intersection(chapterNodeIds, sourceChapterIds);
            if (CollectionUtil.isNotEmpty(intersection)) {
                log.info("更新准备资源ID：{}", po.getId());
                po.setUpdateTime(po.getUpdateTime() + 1);
                baseMapper.updateById(po);
                ThreadUtil.sleep(1000);
            }
        }
    }
}
